<template>
  <div>
    <div class="top-box">
      <div class="bg-box"><img src="../../../../assets/img/logo-my.png" alt="" width="100%"></div>
      <div class="content-box">
        <div class="user">
          <img :src="user.headimgurl ? user.headimgurl : avatar" alt="" width="78" height="78">
          <p>{{ user.name }}</p>
        </div>
      </div>
      <div class="message">
        <router-link to="/message">
          <div class="icon-box"><img src="../../../../assets/img/icon-message.png" alt="" width="100%"></div>
          <div class="dot" v-if="showDot"></div>
        </router-link>
      </div>
    </div>
    <div class="menu">
      <router-link v-for="(item, index) in menus" :key="index" :to="item.path">
        <div class="menu-item">
          <div class="menu-content">
            <div class="icon-title">
              <div class="icon"><img :src="item.icon" alt="" width="100%"></div>
              <div class="title">{{ item.title }}</div>
              <div class="dot" style="right: -12px;" v-if="item.dot && leaveDot"></div>
            </div>
            <div class="arrow"><img src="../../../../assets/img/icon-arrow.png" alt="" width="100%"></div>
          </div>
          <van-divider/>
        </div>
      </router-link>
    </div>
  </div>
</template>

<script>
// import { mapGetters } from 'vuex'
import api from '@/api'
export default {
  name: 'index',
  data () {
    return {
      menus: [
        {path: '/index', icon: require('@/assets/img/icon-menu-course.png'), title: '我的课程'},
        {path: '/studentList', icon: require('@/assets/img/icon-menu-student.png'), title: '我的学生'},
        {path: '/leaveList', icon: require('@/assets/img/icon-menu-leave.png'), title: '审批请假', dot: true},
        {path: '/toCheckStudentList', icon: require('@/assets/img/icon-punch.png'), title: '练琴打卡'},
        {path: '/checkRecord', icon: require('@/assets/img/icon-menu-check.png'), title: '上课签到'},
        {path: '/message', icon: require('@/assets/img/icon-menu-message.png'), title: '消息管理'},
        {path: '/editPoster', icon: require('@/assets/img/icon-menu-share.png'), title: '分享海报'}
      ],
      user: {},
      avatar: require('@/assets/img/icon-avatar.png'),
      showDot: false,
      leaveDot: false
    }
  },
  methods: {
    getInfo () {
      api.teacherPersonal({
        params: {token: localStorage.getItem('teacher_token')},
        callback: res => {
          if (res.data.error_code === 0) {
            sessionStorage.setItem('user', JSON.stringify(res.data.data))
            this.user = res.data.data
            if (localStorage.getItem('userInfo') && !res.data.data.head_sculpture) {
              this.user.headimgurl = JSON.parse(localStorage.getItem('userInfo')).headimgurl
            }
            if (res.data.data.head_sculpture) {
              this.user.headimgurl = res.data.data.head_sculpture
            }
            this.getDot()
            this.getLeaveList()
          } else {
            localStorage.removeItem('teacher_token')
            localStorage.removeItem('userInfo')
            this.$router.push('/')
          }
        }
      })
    },
    getDot () {
      api.teacherMessagIindex({
        params: {id: JSON.parse(sessionStorage.getItem('user')).id},
        callback: res => {
          if (res.data.error_code === 0) {
            Object.keys(res.data.data).forEach(key => {
              if (key.indexOf('new') !== -1 && res.data.data[key] === 2) {
                this.showDot = true
              }
            })
          }
        }
      })
    },
    getLeaveList () {
      let params = {teacher_id: JSON.parse(sessionStorage.getItem('user')).id}
      api.teacherLeaveList({
        params,
        callback: res => {
          if (res.data.error_code === 0) {
            let arr1 = []
            let arr2 = []
            res.data.data.forEach(item => {
              if (item.status === 3) {
                arr1.push(item)
              } else {
                arr2.push(item)
              }
            })
            if (arr1.length) {
              this.leaveDot = true
            }
          }
        }
      })
    }
  },
  computed: {
    // ...mapGetters([
    //   'teacherToken',
    //   'teacherInfo'
    // ])
  },
  created () {
    this.getInfo()
    // if (!this.teacherInfo) {
    //   this.$store.commit('SET_TEACHER', this.user)
    //   this.$store.commit('SET_TOKEN', this.user.token)
    // }
  }
}
</script>

<style scoped>
  .top-box{
    position: relative;
  }
  .top-box img{
    display: block;
  }
  .top-box .content-box{
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding-top: 16px;
    box-sizing: border-box;
  }
  .content-box .user{
    text-align: center;
    font-family: PingFangBlod;
    color: #1E1E1E;
    font-size: 18px;
  }
  .user img{
    border-radius: 50%;
    overflow: hidden;
  }
  .message{
    position: absolute;
    top: 16px;
    right: 10px;
    width: 30px;
    height: 30px;
  }
  .message .icon-box{
    padding: 3px;
    box-sizing: border-box;
    background-color: rgba(0,0,0,.3);
    border-radius: 50%;
    overflow: hidden;
  }
  .message .dot{
    width: 8px;
    height: 8px;
    position: absolute;
    background-color: #ee0a24;
    overflow: hidden;
    border-radius: 50%;
    top: 0;
    right: 0;
  }
  .menu{
    padding-bottom: 50px;
  }
  .menu-item{
    height: 76px;
  }
  .menu-item .van-divider{
    margin: 0;
  }
  .menu-content{
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px;
    height: 75px;
  }
  .menu-content .icon-title{
    display: flex;
    align-items: center;
    position: relative;
  }
  .icon-title .icon{
    width: 28px;
    height: 28px;
    margin-right: 20px;
  }
  .icon-title .title{
    color: #1E1E1E;
    font-size: 16px;
    font-family: PingFangBlod;
  }
</style>
